package codegen.graphnetwork.convertor;

import java.util.Collection;
import org.jgrapht.DirectedGraph;
import org.jgrapht.EdgeFactory;
import org.jgrapht.graph.DefaultDirectedGraph;
import org.netbeans.api.visual.graph.GraphPinScene;
import codegen.graphnetwork.model.*;
/**
 *
 * @author cuong.nguyenmanh2
 */
public class NBVToJGTConvertor {

    public DirectedGraph<Value, TLink> pinGraphToDirectedGraph(GraphPinScene<Node, Link, Value> graph) {
        DefaultDirectedGraph<Value, TLink> resultGraph = new DefaultDirectedGraph<Value, TLink>(new NbEdgeFactory());

        for (Node aNode : graph.getNodes()) {
            Collection<Value> values = graph.getNodePins(aNode);
            for (Value v : values) {
                resultGraph.addVertex(v);
                // Add dependency between input and output of a Node
                if (v.isInput) {
                    for (Value v2 : values) {
                        if (!v2.isInput) {
                            resultGraph.addEdge(v, v2);
                        }
                    }
                }
            }
        }
        
        for (Link aLink:graph.getEdges()){
            resultGraph.addEdge(graph.getEdgeSource(aLink), graph.getEdgeSource(aLink));
        }
        
        return resultGraph;
    }

    class NbEdgeFactory implements EdgeFactory<Value, TLink> {

        public TLink createEdge(Value v, Value v1) {
            return new TLink();
        }
    }

}
